/*
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.util;

import java.util.BitSet;

/**
 * This class is a high speed implementation BitSet.
 * It supports only certain operations and only 9 Bits!  
 * @author Emanuel Moecklin
 */
public class BitSetSkeleton extends BitSet {
	
	private static final long serialVersionUID = -9031147559065149138L;
	
	private static final int MIN_BIT = 1;
	private static final int MAX_BIT = 9;

	private boolean[] bits = new boolean[10];

	@Override
	public int cardinality() {
		int card = bits[0] ? 1 : 0;
		card += bits[1] ? 1 : 0;
		card += bits[2] ? 1 : 0;
		card += bits[3] ? 1 : 0;
		card += bits[4] ? 1 : 0;
		card += bits[5] ? 1 : 0;
		card += bits[6] ? 1 : 0;
		card += bits[7] ? 1 : 0;
		card += bits[8] ? 1 : 0;
		card += bits[9] ? 1 : 0;
		return card;
	}
	
	@Override
	public boolean get(int pos) {
		return bits[pos];
	}
	
	@Override
	public boolean isEmpty() {
		return cardinality()==0;
	}
	
	@Override
	public int nextClearBit(int startIndex) {
		for (int i=startIndex; i<=MAX_BIT; i++) {
			if (!bits[i]) return i; 
		}
		return -1;
	}
	
	@Override
	public int nextSetBit(int startIndex) {
		for (int i=startIndex; i<=MAX_BIT; i++) {
			if (bits[i]) return i; 
		}
		return -1;
	}
	
	@Override
	public void set(int pos) {
		bits[pos] = true;
	}
	
	@Override
	public void set(int pos, boolean value) {
		bits[pos] = value;
	}
	
	@Override
	public String toString() {
		StringBuffer result = new StringBuffer();
		for (int i=MIN_BIT; i<=MAX_BIT; i++) {
			result.append(get(i)?"1":"0"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		return result.toString();
	}
	
}
